package com.lingshi.shopping_category_service.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lingshi.shopping.constant.Constant;
import com.lingshi.shopping.constant.RedisKey;
import com.lingshi.shopping.entity.Category;
import com.lingshi.shopping.service.CategoryService;
import com.lingshi.shopping_category_service.mapper.CategoryMapper;
import istio.v1.auth.Ca;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.Arrays;
import java.util.List;

@DubboService
public class CategoryServiceImpl implements CategoryService {


    @Autowired
    private RedisTemplate redisTemplate;


    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public void add(Category category) {
        categoryMapper.insert(category);
        //同步数据
        syncCategoryMySQLToRedis();
    }

    @Override
    public void update(Category category) {
        categoryMapper.updateById(category);
        //同步数据
        syncCategoryMySQLToRedis();
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        Category category = new Category();
        category.setId(id);
        category.setStatus(status);
        categoryMapper.updateById(category);

        //同步数据
        syncCategoryMySQLToRedis();
    }

    @Override
    public void delete(Long[] ids) {
        categoryMapper.deleteBatchIds(Arrays.asList(ids));

        //同步数据
        syncCategoryMySQLToRedis();
    }

    @Override
    public Category findById(Long id) {
        return categoryMapper.selectById(id);
    }

    @Override
    public Page<Category> search(int page, int size) {
        Page<Category> pageObj = new Page<>(page, size);
        categoryMapper.selectPage(pageObj, null);
        return pageObj;
    }

    @Override
    public List<Category> findAll() {
        /**
         * 数据缓存思路
         * 1，先从redis查询数据 list集合
         *   有：直接返回
         *   没有：
         *    从数据库查询，并且存储到redis
         */

        ListOperations<String, Category> los = redisTemplate.opsForList();
        //先从redis查询数据 list集合
        List<Category> categories = los.range(RedisKey.CATEGORIES, 0, -1);

        if (CollUtil.isEmpty(categories)) {
            LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Category::getStatus, Constant.ENABLE);
            categories = categoryMapper.selectList(queryWrapper);

            //存储到redis
            los.leftPushAll(RedisKey.CATEGORIES, categories);
        }

        return categories;
    }

    /**
     * 同步广告数据 MySQL->Redis
     */
    private void syncCategoryMySQLToRedis() {
        ListOperations<String, Category> los = redisTemplate.opsForList();
        redisTemplate.delete(RedisKey.CATEGORIES);

        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getStatus, Constant.ENABLE);
        List<Category> categories = categoryMapper.selectList(queryWrapper);

        //存储到redis
        los.leftPushAll(RedisKey.CATEGORIES, categories);
    }
}
